import format from '@dfeidao/atom-web/dt/format';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';
import p001 from './p001';
import p003 from './p003';
import p004 from './p004';
export default async function a001(fd: IFeidaoAiBrowserComponent) {
	const productid = fd.data.node.querySelector<HTMLSpanElement>('[data-feidao-id="user"]').getAttribute('prid');
	const page_no = 1;
	const page_size = 20;
	const resume_name = get(fd, 'search_name');
	const op_cont = get(fd, 'search_cont');

	const asc = get(fd, 'asc') as string[];
	const desc = get(fd, 'desc') as string[];


	const qwe = new Date(new Date().toLocaleDateString()).getTime();
	const begin_time = format(qwe, 0);
	const todayYear = (new Date()).getFullYear();
	const todayMonth = (new Date()).getMonth();
	const todayDay = (new Date()).getDate();
	const todayDay_time = (new Date(todayYear, todayMonth, todayDay, 23, 59, 59)).getTime();
	const end_time = format(todayDay_time, 0);
	const begin = fd.data.node.querySelector<HTMLInputElement>('[id="time_begin"]');
	const end = fd.data.node.querySelector<HTMLInputElement>('[id="time_end"]');

	if (!begin.value) { begin.value = begin_time; }
	if (!end.value) { end.value = end_time; }
	const operation_time = { $gte: get(fd, 'begin_time') ? get(fd, 'begin_time') : qwe, $lte: get(fd, 'end_time') ? get(fd, 'end_time') : todayDay_time };
	const query = {
		filter: {
		} as {
			resume_name: {};
			op_cont: {};
			operation_time: {};
			productid: {};
		},
		page_no,
		page_size,
		asc,
		desc
	};
	if (resume_name) {
		query.filter.resume_name = resume_name;
	}
	if (op_cont) {
		query.filter.op_cont = op_cont;
	}
	if (operation_time) {
		query.filter.operation_time = operation_time;
	}

	if (asc) {
		query.asc = asc;
	} else {
		query.asc = [];
	}
	if (desc) {
		query.desc = desc;
	} else { query.desc = ['operation_time']; }
	if (productid) {
		query.filter.productid = productid;
	}
	if (get(fd, 'page_no')) {
		query.page_no = get(fd, 'page_no');
	}
	const res = await nodejs<{ count: number, dataArry: Array<{}> }>('operation/zj-000001/s001', query);
	const a = fd.data.node.querySelector<HTMLDivElement>('[id="a"]');
	const b = fd.data.node.querySelector<HTMLDivElement>('[id="b"]');
	const fenye = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p004"]');
	if (res.count === 0) {
		a.classList.remove('display-n');
		b.classList.add('display-n');
		fenye.classList.add('display-n');
		render(fd, '', p003, 'p003', "inner");
	} else {
		a.classList.add('display-n');
		b.classList.remove('display-n');
		render(fd, res.dataArry, p001, 'p001', 'inner');
		if (res.count > 20) { render(fd, res, p004, 'p004', 'inner'); fenye.classList.remove('display-n'); } else { fenye.classList.add('display-n'); }
	}

}
